﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Proggy.Data.Entities;

namespace Proggy.Web.Cms.ViewModels
{
    public class SortChildContentViewModel
    {
        public PublishableNode ParentNode { get; set; }
        public List<PublishableNode> ChildNodesOrdered { get; set; }
        public bool HasChildren { get; set; }
        public bool ParentExists { get; set; }
        public int RequestedParentId { get; set; }
        public int[] PostSortedChildren { get; set; }

        public void ReOrderChildrenByPostSortedChildren()
        {
            for (var i = 0; i < PostSortedChildren.Length; i++)
            {
                // get child
                var child = this.ChildNodesOrdered.Single(x => x.PublishableNodeId == PostSortedChildren[i]);
                
                // set 
                child.OrderInParent = i;
            }

            // reorder
            this.ChildNodesOrdered = this.ChildNodesOrdered.OrderBy(x => x.OrderInParent).ToList();
        }
    }
}
